-
Notifications
You must be signed in to change notification settings - Fork 23
/
PrintType.scala
257 lines (228 loc) · 12.1 KB
/
PrintType.scala
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
import scala.language.experimental.macros
import scala.reflect.macros.blackbox.Context
import scala.util.{ Try => ScalaTry }
object PrintType {
// These macros deal with two types of data structure:
//
// - Types - objects representing the type of a variable
// or expression;
//
// - Symbols - objects representing class, trait, method,
// or variable declarations.
//
// We obtain `Types` from type parameters on the macro
// itself, and `Symbols` via `Types` or `Trees`, subject
// to a couple of restrictions:
//
// - we can only access `Symbol` information from a `Type`
// if we have enough information at compile time on how
// the type parameter is bound. For example, if we're
// inspecting a type parameter from a method definition,
// we don't yet know what types the method will be called
// with:
//
// Example: def genericMethod[A] = genericMacro[A]
//
// - we can only access `Symbol` information from `Trees`
// of type `SymTree`. These are essentially: trees
// representing declarations, and trees representing
// references to types and terms.
// Macro that generates a `println` statement to print
// information about the structure of type `A`. This
// includes any type constructor and bound or unbound
// type parameters:
def printStructure[A]: Unit =
macro PrintTypeMacros.printStructureMacro[A]
// Macro that generates a `println` statement to print
// declaration information of type `A`.
//
// This only prints meaningful output if we can inspect
// `A` to get at its definition:
def printSymbol[A]: Unit =
macro PrintTypeMacros.printTypeSymbolMacro[A]
// Macro that generates a `println` statement to print
// declaration information about the type of a `value`.
//
// This only prints meaningful output if `value` is
// represented by an expression that can be linked to
// a declaration. Examples include method calls, field
// references, identifiers, and method, field, class,
// and trait declarations:
def printSymbol(value: Any): Unit =
macro PrintTypeMacros.printTermSymbolMacro
// Macro that generates a `println` statement to print
// information about each member declaration of type `A`.
//
// This only prints meaningful output if we can inspect
// `A` to get at its definition:
def printDecls[A]: Unit =
macro PrintTypeMacros.printDeclsMacro[A]
}
class PrintTypeMacros(val c: Context) {
import c.universe._
def printStructureMacro[A: c.WeakTypeTag] = {
val tpe = weakTypeOf[A]
q"""
println(${show(tpe)} + ".typeConstructor " + ${show(tpe.typeConstructor)})
println(${show(tpe)} + ".typeArgs " + ${show(tpe.typeArgs)})
println(${show(tpe)} + ".dealias.typeConstructor " + ${show(tpe.dealias.typeConstructor)})
println(${show(tpe)} + ".dealias.typeArgs " + ${show(tpe.dealias.typeArgs)})
println(${show(tpe)} + ".dealias.typeConstructor.typeParams " + ${show(tpe.dealias.typeConstructor.typeParams)})
println(${show(tpe)} + ".typeSymbol " + ${show(tpe.typeSymbol)})
"""
}
def printTypeSymbolMacro[A: c.WeakTypeTag]: c.Tree =
printSymbol(weakTypeOf[A].typeSymbol, "")
def printTermSymbolMacro(value: c.Tree): c.Tree =
printSymbol(value.symbol, "")
def printDeclsMacro[A: c.WeakTypeTag]: c.Tree = {
val tpe = weakTypeOf[A]
q"""
println(${show(tpe)})
..${tpe.decls.map(printSymbol(_, " "))}
"""
}
// Generate a print statement that prints information about
// a `Symbol`. This essentially calls all the simple methods
// from the `Symbol` API.
//
// There is a type hierarchy for symbols:
// - Symbol
// - TermSymbol - any term (value-level) declaration
// - MethodSymbol - any method (or constructor or field) declaration
// - ModuleSymbol - any singleton object declaration
// - TypeSymbol - any type-level declaration
// - ClassSymbol - class or trait declaration
//
// The code below works out what type the argument is, and prints
// everything it can for that type:
def printSymbol(decl: Symbol, prefix: String = ""): c.Tree = {
// The code below buffers `println` statements in this mutable list.
//
// The final output of the macro is a block containing a sequence
// of all the accumulated `printlns`:
var exprs = Seq.empty[c.Tree]
def write(str: String) =
exprs = exprs :+ q"println($str)"
// Print the name of the symbol:
write(prefix + showDecl(decl))
// Things we know for any `Symbol`:
{
var keywords = Seq.empty[String]
if(decl.isTerm) keywords = keywords :+ "isTerm"
if(decl.isType) keywords = keywords :+ "isType"
if(decl.isClass) keywords = keywords :+ "isClass"
if(decl.isMethod) keywords = keywords :+ "isMethod"
if(decl.isModule) keywords = keywords :+ "isModule"
if(decl.isAbstract) keywords = keywords :+ "isAbstract"
if(decl.isAbstractOverride) keywords = keywords :+ "isAbstractOverride"
if(decl.isFinal) keywords = keywords :+ "isFinal"
if(decl.isImplementationArtifact) keywords = keywords :+ "isImplArtifact"
if(decl.isImplicit) keywords = keywords :+ "isImplicit"
if(decl.isJava) keywords = keywords :+ "isJava"
if(decl.isMacro) keywords = keywords :+ "isMacro"
if(decl.isPackage) keywords = keywords :+ "isPackage"
if(decl.isPackageClass) keywords = keywords :+ "isPackageClass"
if(decl.isParameter) keywords = keywords :+ "isParameter"
if(decl.isPrivate) keywords = keywords :+ "isPrivate"
if(decl.isPrivateThis) keywords = keywords :+ "isPrivateThis"
if(decl.isProtected) keywords = keywords :+ "isProtected"
if(decl.isProtectedThis) keywords = keywords :+ "isProtectedThis"
if(decl.isPublic) keywords = keywords :+ "isPublic"
if(decl.isSpecialized) keywords = keywords :+ "isSpecialized"
if(decl.isStatic) keywords = keywords :+ "isStatic"
if(decl.isSynthetic) keywords = keywords :+ "isSynthetic"
if(decl.isModuleClass) keywords = keywords :+ "isModuleClass"
if(decl.alternatives.nonEmpty) keywords = keywords :+ "alternatives = " + decl.alternatives
if(decl.annotations.nonEmpty) keywords = keywords :+ "annotations = " + decl.annotations
if(true) keywords = keywords :+ "fullName = " + decl.fullName
if(true) keywords = keywords :+ "info = " + decl.info.toString.replaceAll("\n *", " ")
if(decl.overrides.nonEmpty) keywords = keywords :+ "overrides = " + decl.overrides
if(true) keywords = keywords :+ "owner = " + decl.owner
if(decl.companion != NoSymbol) keywords = keywords :+ "companion = " + decl.companion
if(true) keywords = keywords :+ "typeSignature = " + decl.typeSignature.toString.replaceAll("\n *", " ")
write(prefix + " asSymbol")
keywords.foreach(kw => write(prefix + " " + kw))
}
// Things that are defined for any `TypeSymbol`:
if(decl.isType) {
val tipe = decl.asType
var keywords = Seq.empty[String]
if(tipe.isAliasType) keywords = keywords :+ "isAliasType"
if(tipe.isContravariant) keywords = keywords :+ "isContravariant"
if(tipe.isCovariant) keywords = keywords :+ "isCovariant"
if(tipe.isExistential) keywords = keywords :+ "isExistential"
if(true) keywords = keywords :+ "toType = " + tipe.toType
if(true) keywords = keywords :+ "toTypeConstructor = " + tipe.toTypeConstructor
if(tipe.typeParams.nonEmpty) keywords = keywords :+ "typeParams = " + tipe.typeParams
write(prefix + " asType")
keywords.foreach(kw => write(prefix + " " + kw))
}
// Things that are defined for any `TermSymbol`:
if(decl.isTerm) {
val term = decl.asTerm
var keywords = Seq.empty[String]
if(term.isAccessor) keywords = keywords :+ "isAccessor"
if(term.isByNameParam) keywords = keywords :+ "isByNameParam"
if(term.isCaseAccessor) keywords = keywords :+ "isCaseAccessor"
if(term.isGetter) keywords = keywords :+ "isGetter"
if(term.isLazy) keywords = keywords :+ "isLazy"
if(term.isOverloaded) keywords = keywords :+ "isOverloaded"
if(term.isParamAccessor) keywords = keywords :+ "isParamAccessor"
if(term.isParamWithDefault) keywords = keywords :+ "isParamWithDefault"
if(term.isSetter) keywords = keywords :+ "isSetter"
if(term.isStable) keywords = keywords :+ "isStable"
if(term.isVal) keywords = keywords :+ "isVal"
if(term.isVar) keywords = keywords :+ "isVar"
if(term.getter != NoSymbol) keywords = keywords :+ "getter = " + term.getter
if(term.setter != NoSymbol) keywords = keywords :+ "setter = " + term.setter
ScalaTry {
if(term.accessed != NoSymbol) keywords = keywords :+ "accessed = " + term.accessed
}
write(prefix + " asTerm")
keywords.foreach(kw => write(prefix + " " + kw))
}
// Things that are defined for any `ClassSymbol`:
if(decl.isClass) {
val clss = decl.asClass
var keywords = Seq.empty[String]
if(clss.isCaseClass) keywords = keywords :+ "isCaseClass"
if(clss.isDerivedValueClass) keywords = keywords :+ "isDerivedValueClass"
if(clss.isNumeric) keywords = keywords :+ "isNumeric"
if(clss.isPrimitive) keywords = keywords :+ "isPrimitive"
if(clss.isSealed) keywords = keywords :+ "isSealed"
if(clss.isTrait) keywords = keywords :+ "isTrait"
if(clss.baseClasses.nonEmpty) keywords = keywords :+ "baseClasses = " + clss.baseClasses
if(clss.knownDirectSubclasses.nonEmpty) keywords = keywords :+ "knownDirectSubclasses = " + clss.knownDirectSubclasses
if(clss.module != NoSymbol) keywords = keywords :+ "module = " + clss.module
if(clss.primaryConstructor != NoSymbol) keywords = keywords :+ "primaryConstructor = " + clss.primaryConstructor
if(clss.typeParams.nonEmpty) keywords = keywords :+ "typeParams = " + clss.typeParams
if(clss.selfType != clss.toType) keywords = keywords :+ "selfType = " + clss.selfType
write(prefix + " asClass")
keywords.foreach(kw => write(prefix + " " + kw))
}
// Things that are defined for any `MethodSymbol`:
if(decl.isMethod) {
val meth = decl.asMethod
var keywords = Seq.empty[String]
if(meth.isConstructor) keywords = keywords :+ "isConstructor"
if(meth.isPrimaryConstructor) keywords = keywords :+ "isPrimaryConstructor"
if(meth.isVarargs) keywords = keywords :+ "isVarargs"
if(meth.exceptions.nonEmpty) keywords = keywords :+ "exceptions = " + meth.exceptions
if(meth.paramLists.nonEmpty) keywords = keywords :+ "paramLists = " + meth.paramLists
if(true) keywords = keywords :+ "returnType = " + meth.returnType
if(meth.typeParams.nonEmpty) keywords = keywords :+ "typeParams = " + meth.typeParams
write(prefix + " asMethod")
keywords.foreach(kw => write(prefix + " " + kw))
}
// Things that are defined for any `ModuleSymbol`:
if(decl.isModule) {
val modl = decl.asModule
var keywords = Seq.empty[String]
if(modl.moduleClass != NoSymbol) keywords = keywords :+ "moduleClass"
write(prefix + " asModule")
keywords.foreach(kw => write(prefix + " " + kw))
}
q"..$exprs"
}
}